//
//  ImageCollectionViewCell.m
//  Btpin
//
//  Created by Vic on 2019/3/24.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "ImageCollectionViewCell.h"

@interface ImageCollectionViewCell ()
{
    UIButton *imgBtn;
    UIButton *deleteBtn;
    UILabel *statusLbl;
}
@end

@implementation ImageCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
- (void)initUI {
    imgBtn = [UIToolsControl initButtonWithFrame:self.bounds imageName:nil target:self action:@selector(imgBtnTap:) title:nil titleColor:nil];
//    [imgBtn setBackgroundImage:[UIImage imageNamed:@"update_icon"] forState:UIControlStateNormal];
    imgBtn.backgroundColor = THEMECOLOR_OF_GRAY;
    [self.contentView addSubview:imgBtn];
    imgBtn.userInteractionEnabled = NO;
    
    statusLbl = [UIToolsControl initLabelWithFrame:CGRectMake(0, self.bounds.size.height-27*Scale, 60*Scale, 27*Scale) text:nil textColor:COLOR_OF_WHITE font:Font_15];
    statusLbl.font = FontBold(Font_15);
    statusLbl.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:statusLbl];
    
    deleteBtn = [UIToolsControl initButtonWithFrame:CGRectMake(self.frame.size.width-40*Scale, 0, 40*Scale, 40*Scale) imageName:@"close_icon" target:self action:@selector(deleteBtnTap:) title:nil titleColor:nil];
    [self.contentView addSubview:deleteBtn];
    deleteBtn.hidden = YES;
    deleteBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 26*Scale, 26*Scale, 0);
}
- (void)reloadData:(nullable ImageModel *)model indexPath:(NSIndexPath *)indexPath lastObject:(BOOL)lastObject hiddenAudit:(BOOL)hiddenAudit {
    imgBtn.indexPath = indexPath;
    deleteBtn.indexPath = indexPath;
    
    if (lastObject) {
        deleteBtn.hidden = YES;
        [imgBtn setBackgroundImage:[UIImage imageNamed:@"update_icon"] forState:UIControlStateNormal];
    } else {
        deleteBtn.hidden = NO;
        if (model) {
            [imgBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:model.img] forState:UIControlStateNormal];
            
            NSString *text = @"审核中";
            UIColor *color = THEMECOLOR_OF_TEXT_cccccc;
            if ([model.audit integerValue] == 1) {
                text = @"已审核";
                color = THEMECOLOR_OF_ORANGE;
                deleteBtn.hidden = YES;
            } else if ([model.audit integerValue] == 2) {
                text = @"未审核";
                color = THEMECOLOR_OF_TEXT_cccccc;
            } else if ([model.audit integerValue] == 3) {
                text = @"未通过";
                color = THEMECOLOR_OF_TEXT_cccccc;
            }
            statusLbl.text = text;
            statusLbl.backgroundColor = color;
            statusLbl.hidden = hiddenAudit;
        }
    }
}
- (void)imgBtnTap:(UIButton *)button {
//    if (self.delegate && [self.delegate respondsToSelector:@selector(imageCollectionViewCellImgTap:)]) {
//        [self.delegate imageCollectionViewCellImgTap:button.indexPath];
//    }
}
- (void)deleteBtnTap:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(imageCollectionViewCellDeleteTap:)]) {
        [self.delegate imageCollectionViewCellDeleteTap:button.indexPath];
    }
}

@end
